var RegistrationID

function initJPush() {
	try{
		// plus.Push.init()
		document.addEventListener("jpush.receiveMessage", receiveMessage, false)
		document.addEventListener("jpush.receiveNotification", receiveNotification, false)
		document.addEventListener("jpush.openNotification", openNotification, false)
		// getRegistrationID();
	}catch(e){
		console.log(e)
	}
}

function reduceBadge() {
	// window.plus.Push.getApplicationIconBadgeNumber(function(data) {
	// 	if (data) {
	// 		var badge = data - 1;
	// 		window.plus.Push.setBadge(badge);
	// 		window.plus.Push.setApplicationIconBadgeNumber(badge);
	// 	}
	// });
}

/**
 * JPush 获取自定义消息内容
 * 
 * receiveMessage {"content":"asdasd","extras":{"a":"a","b":"b"},"_j_msgid":9007250639988316}
 */
function receiveMessage() {
	// var content
	// if (plus.os.name == 'iOS') {
	// 	content = window.plus.Push.receiveNotification.aps.alert
	// } else {
	// 	content = window.plus.Push.receiveNotification.alert
	// }
	// console.log('receiveNotification' + JSON.stringify(window.plus.Push.receiveNotification))
}

/**
 * JPush 获取收到通知内容 
 * 
 * receiveNotification{"_j_msgid":20266256379193020,"_j_business":1,"c":"3","_j_uid":36829955797,"aps":{"alert":{"title":"测试","body":"您好测试"},"badge":1,"sound":"default"},"a":"1","b":"2"}
 */
function receiveNotification() {
	// var content
	// if (plus.os.name == 'iOS') {
	// 	content = window.plus.Push.receiveNotification.aps.alert
	// } else {
	// 	content = window.plus.Push.receiveNotification.alert
	// }
	// console.log('receiveNotification' + JSON.stringify(window.plus.Push.receiveNotification))
}

/**
 * JPush 获取点击通知内容
 * 
 * openNotification{"_j_msgid":20266256379193020,"_j_business":1,"c":"3","_j_uid":36829955797,"aps":{"alert":{"title":"测试","body":"您好测试"},"badge":1,"sound":"default"},"a":"1","b":"2"}
 */
function openNotification() {
	reduceBadge();
	var content
	if (plus.os.name == 'iOS') {
		content = plus.Push.openNotification.aps.alert
	} else {
		content = plus.Push.openNotification.alert
	}
	var msg = window.plus.Push.openNotification;
	console.log('JPushTools openNotification' + JSON.stringify(msg))
	handleBusiness(msg)
}

/**
 * 点击推送启动应用的时候原生会将该 notification 缓存起来，该方法用于获取缓存 notification
 * 退出程序后收到推送通知，此时点击通知事件下发，应用若不能及时注册点击通知事件，可调用此方法获取点击内容
 */
function getLaunchAppCacheNotification() {
	plus.Push.getLaunchAppCacheNotification(function(notification) {
		console.log("getLaunchAppCacheNotification" + JSON.stringify(notification));
		if (notification) {
			reduceBadge();
			window.plus.Push.clearLaunchAppCacheNotification()
			handleBusiness(notification)
		}
	})
}

function getRegistrationID() {
	var jpushInterval = setInterval(function() {
		plus.Push.getRegistrationID(function(data) {
			console.log('getRegistrationID: ' + data)
			if (data.length != 0) {
				clearInterval(jpushInterval)
				RegistrationID = data;
				var state = app.getState();
				if (state.user && state.token) {
					// 用户绑定RegistrationID
					ajaxGet(BASE_URL + 'cc/user/updateRegistrationId', {
						EnginnerId: state.enginnerId,
						registrationId: data
					}, function(data) {
						if (data.code === 200) {
							console.log("用户绑定RegistrationID成功")
						} else {
							getRegistrationID()
							console.log("用户绑定RegistrationID失败")
						}
					}, function(errorMsg) {
						getRegistrationID()
						console.log("用户绑定RegistrationID失败")
					})
				}
			}
		})
	}, 1000);

}

/**
 * 初始化推送
 */
function initPush() {
	setTimeout(initJPush, 2000);
}

var pushClickTimeStamp = 0;

/**
 * 处理业务
 * 
 * @param {Object} notification
 */
function handleBusiness(notification){
	
	// 获取首页webview
	var main = plus.webview.getWebviewById("main");
	// if (!mian) {
	// 	return;
	// }
	var extra = notification.extra;
	var state = app.getState();
	if (state.user && state.token && extra) {
		// 获取推送类型
		var type = extra.type
		var topWebviewId = '';
		var topWebview = plus.webview.getTopWebview();
		if (topWebview) {
			topWebviewId = topWebview.id;
		}
		
		// 待派工状态值 type = 1
		// TL待派工提醒---------待指派页面
		if (type == '1') { 
			if (topWebviewId != "view/service/notice/toBeAssignedList.html") {
				app.jumpTo('/view/service/notice/toBeAssignedList.html','view/service/notice/toBeAssignedList.html')
			}
		} 
		// 转派其它TL状态值 type = 2
		else if (type == '2') { 
			if (topWebviewId != "view/service/notice/toBeAssignedList.html") {
				app.jumpTo('/view/service/notice/toBeAssignedList.html','view/service/notice/toBeAssignedList.html')
			}
		}
		// FSE拒绝服务单状态值 type = 3
		else if (type == '3') { 
			if (topWebviewId != "view/service/notice/toBeAssignedList.html") {
				app.jumpTo('/view/service/notice/toBeAssignedList.html','view/service/notice/toBeAssignedList.html')
			}
		}
		// FSE待接受服务单状态值 type = 4
		else if (type == '4') { 
			if (topWebviewId != "view/service/notice/toBeAcceptedList.html") {
				var indexFSE = plus.webview.getWebviewById('view/main/indexFSE.html');
				if(indexFSE){
					indexFSE.evalJS('getList("0")')
				}
				// app.jumpTo('/view/service/notice/toBeAcceptedList.html','view/service/notice/toBeAcceptedList.html')
				var toBeAcceptedList = plus.webview.create('/view/service/notice/toBeAcceptedList.html','view/service/notice/toBeAcceptedList.html')
				toBeAcceptedList.show()
			}
		}
		// 待上门状态值 type = 5
		else if (type == '5') { 
			if (topWebviewId != "view/service/notice/acceptedList.html") {
				app.jumpTo('/view/service/notice/acceptedList.html','view/service/notice/acceptedList.html')
			}
		}
		// 待签到状态值 type = 6
		else if (type == '6') { 
			if (topWebviewId != "view/service/notice/acceptedList.html") {
				app.jumpTo('/view/service/notice/acceptedList.html','view/service/notice/acceptedList.html')
			}
		}
		// 待签离状态值 type = 7
		else if (type == '7') { 
			if (topWebviewId != "view/service/notice/checkedInList.html") {
				app.jumpTo('/view/service/notice/checkedInList.html','view/service/notice/checkedInList.html')
			}
		}
		// 待填写日志状态值 type = 8
		else if (type == '8') { 
			
		}
	}
}
